﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MedicalCare.DbLayer.Adaptors;
using StructureMap;
using MedicalCare.Messages;
using MedicalCare.EntityDefinitions.Entities;
using MedicalCare.Utilities;

namespace MedicalCare.Admin
{
    public partial class EquipmentDetail : System.Web.UI.Page
    {
        private IEquipmentTypeAdaptor _eptAdaptor;
        private IEquipmentsAdaptor _epAdaptor;
        //Champion 2013/1/3
        private int equipmentsId { get { return Convert.ToInt32(ViewState["equipmentsId"]); } set { ViewState["equipmentsId"] = value; } }
        private string action { get { return Convert.ToString(ViewState["action"]); } set { ViewState["action"] = value; } }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Champion 2013/1/3
                loadEquipmentType();
                action = Request.Params["action"].ToString();
                
                if (ActionType.NEW.ToString().Equals(action))
                {
                    cleanPage();
                }
                else if (ActionType.EDIT.ToString().Equals(action))
                {
                    loadEquipmentDetail();
                }
            }
        }

        //Champion 2013/1/3
        private void loadEquipmentDetail()
        {
            _epAdaptor = ObjectFactory.GetInstance<IEquipmentsAdaptor>();
            equipmentsId = Convert.ToInt32(Request.Params["Id"].ToString());
            EquipmentsEntity equipmentsEntity = _epAdaptor.GetEquipmentById(equipmentsId);
            tbSensorId.Text = equipmentsEntity.SensorId;
            tbEquipmentName.Text = equipmentsEntity.EquipmentName;
            tbModel.Text = equipmentsEntity.Model;
            tbManufacturer.Text = equipmentsEntity.Manufacturer;
            ddlEquipmentTypeName.SelectedValue = equipmentsEntity.EquipmentTypeId.ToString();
        }

        //Champion 2013/1/3
        private void loadEquipmentType()
        {
            _eptAdaptor = ObjectFactory.GetInstance<IEquipmentTypeAdaptor>();
            IList<EquipmentTypeEntity> equlist = _eptAdaptor.GetAllEquipmentType();
            foreach (EquipmentTypeEntity equ in equlist)
            {
                var li = new ListItem();
                li.Text = equ.Name;
                li.Value = equ.Id.ToString();
                ddlEquipmentTypeName.Items.Add(li);
            }
        }
        //Champion 2013/1/3
        private void cleanPage()
        {
            tbSensorId.Text = "";
            tbEquipmentName.Text = "";
            tbModel.Text = "";
            tbManufacturer.Text = "";
        }
        //Champion 2013/1/3
        private EquipmentsMessage getEquipmentsMessage()
        {
          return new EquipmentsMessage()
            {
                Id=equipmentsId,
                SensorId = tbSensorId.Text.ToString(),
                EquipmentTypeId = Convert.ToInt32(ddlEquipmentTypeName.SelectedValue),
                Name = tbEquipmentName.Text.ToString(),
                Model = tbModel.Text.ToString(),
                Manufacturer = tbManufacturer.Text.ToString(),
                CreateUser = AppPage.CurrentUser.UserName,
                Valid="Y"
            };
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var epAdaptor = ObjectFactory.GetInstance<IEquipmentsAdaptor>();
            //Champion 2013/1/3
            var message = getEquipmentsMessage();
            epAdaptor.Update(message);
            Server.Transfer("~/Admin/EquipmentsList.aspx");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Server.Transfer("~/Admin/EquipmentsList.aspx");
        }
    }
}